package database

import (
	"fmt"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func (d *databaseImpl) openSQLiteDB() error {
	db, err := gorm.Open(sqlite.Open(d.dsn), &gorm.Config{
		Logger: d.logger,
		// 关键修改：确保不禁用外键约束
		DisableForeignKeyConstraintWhenMigrating: false,
	})
	if err != nil {
		return fmt.Errorf("数据库连接失败：%v", err)
	}
	sqlDB, err := db.DB()
	if err != nil {
		return fmt.Errorf("无法获取泛型数据库对象：%v", err)
	}
	d.db = db
	d.opened = true
	d.sqlDB = sqlDB
	return nil
}
